#include <iostream>
#include <jsoncpp/json/json.h>
#include <string>
#include <memory>
#include <sstream>
int main()
{
    //序列化
    Json::Value stu;
    stu["name"]="zhangsan";
    stu["age"]=20;
    stu["socre"].append(80);
    stu["socre"].append(89.5);
    stu["socre"].append(99);

    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

    std::stringstream ss;
    int ret=sw->write(stu,&ss);
    if(ret!=0){
        std::cout<<"Serialize failed"<<std::endl;
        return -1;
    }
    std::cout<<"序列化的结果:\n"<<ss.str()<<std::endl;

    //反序列化
    std::string str=ss.str();
    Json::Value root;
    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());

    bool ret1=cr->parse(str.c_str(),str.c_str()+str.size(),&root,nullptr);
    if(!ret1){
        std::cout<<"UnSerialize failed"<<std::endl;
        return -2;
    }
    std::cout << "反序列化结果:\n"
            << "name:" << root["name"].asString() << "\n"
            << "age:" << root["age"].asInt() << "\n"
            << "socre:" << root["socre"][0].asFloat() << " " << root["socre"]
            [1].asInt()
            << " " << root["socre"][2].asFloat() << "\n";

    return 0;
}